# -*- bazel -*-

load("@drake//tools/skylark:cc.bzl", "cc_library")
load("@drake//tools/workspace/gfortran:fortran.bzl", "fortran_library")

licenses(["by_exception_only"])  # SNOPT

# Drake's binary releases are allowed to redistribute SNOPT only if SNOPT is
# not redistributed as a stand-alone package -- users must not use the SNOPT
# code on its own, but rather only through Drake's interfaces.  To that end,
# we (1) always link SNOPT statically, (2) use hidden symbol visibility.

fortran_library(
    name = "fortran_objects",
    srcs = [
        "interfaces/src/snopt_wrapper.f90",
    ] + glob(
        ["src/*.f"],
        exclude = [
            # Only used for SQOPT.
            "src/snopth.f",
            # Not listed in snopt/src/Submakefile.
            "src/sn27lu77.f",
            "src/sn27lu90.f",
            # Use a system build of BLAS.
            "src/snblas.f",
        ],
    ),
    fopts = [
        # Allow our patches/snopt-openmp.patch to mark THREADPRIVATE.
        "-fopenmp",
    ] + [
        # We need this for SNOPT 7.6 which has non-conforming code.
        "-fallow-argument-mismatch",
    ],
    # Link statically per note (1) above.
    linkstatic = 1,
    deps = [
        "@blas",
    ],
    # Always link the entirety of SNOPT.
    alwayslink = 1,
)

cc_library(
    name = "snopt_cwrap",
    srcs = [
        "interfaces/src/snopt_cwrap.c",
    ],
    hdrs = [
        "interfaces/include/snopt.h",
        "interfaces/include/snopt_cwrap.h",
    ],
    includes = [
        "interfaces/include",
    ],
    copts = [
        "-w",
        # Hide symbols per note (2) above.  The snopt_cwrap.c file isn't
        # proprietary (it is published at github.com/snopt/snopt-interface),
        # but perhaps keeping these functions obscured helps serve the overall
        # goal of obscuring the calls into the SNOPT object code even more.
        "-fvisibility=hidden",
    ],
    # Link statically per note (1) above.
    linkstatic = 1,
    visibility = ["//visibility:public"],
    deps = [
        ":fortran_objects",
    ],
    # Always link the entirety of SNOPT.
    alwayslink = 1,
)
